import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入

declare var $: any;
@Component({
   	selector: 'sa-car-panel-history',
   	templateUrl: './car-panel-history.component.html',
})
export class CarPanelHistoryComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	carList:any[] = []; //车辆数据列表
	companyId:string = ''; //公司ID
	companyList:any[] = []; //公司数据列表
	deviceId:string = '';//设备ID
	carNumber:string = ''; //车牌号码
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	
	constructor(private jsonApiService: JsonApiService) {}

	ngOnInit() {
		this.initDate().then(()=>this.load());
		this.selectCompany();
		this.queryCarListByCompanyId().then(()=>this.selectCompany());
	}
	getTime(date){
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var day = date.getDate()  < 10 ? "0" + date.getDate() : date.getDate();
		return year+"-"+month+"-"+day;
	}
	//刷新
	refresh(){
		this.load();
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = that.getTime(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd",
						    language: "zh-CN",
						    autoclose: true,
						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = that.getTime(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
//						var date = new Date();
//						$("#datetimepicker2").val(that.getTime(date));//当前时间
//						date.setDate(date.getDate() - 7);//设置前一天
//						$("#datetimepicker1").val(that.getTime(date));
						resolve();
					});
				}
			);
		})

	}
	//初始化车辆
	selectCompany(){
		System.import('script-loader!select2/dist/js/select2.min.js').then(()=>{
			System.import('assets/js/select2-zh-CN.js').then(()=>{
				var that = this;
		        $("#carNumberSelect2").select2({
			    	language : "zh-CN",
			    	placeholder :"请选择",
			    	allowClear : true,
			    	data : this.carList,//为Select添加数据
			        templateResult: this.formatRepo
			    }).on ("select2:select", function (e) {
		        	  var data = e.params.data;
		        	  that.deviceId = data.id;
		        	  that.carNumber = data.text;
		        });
			})
	  	})
	}
	//select2 格式回调函数
	formatRepo (state) {
		if (!state.id) {return state.text};
		var $state = $('<span><i class="fa fa-car green"></i>&nbsp;&nbsp;' + state.text + '</span>');
		return $state;
	}
	//清除车辆选中值
	del_carNumber(){
		this.deviceId = '';
		this.carNumber = '';
		var str=$("<span class='select2-selection__placeholder'>请选择</span>");
		$('#carNumberSelect2').val("");
		$("#select2-carNumberSelect2-container").empty();
		$("#select2-carNumberSelect2-container").attr("title","");
		$("#select2-carNumberSelect2-container").prepend(str);
	}
	//公司id拉取车辆
	queryCarListByCompanyId(){
		var that = this;
		return new Promise(function (resolve, reject) {
			let reqdata = 'companyId=59bd13a96c8da407601b5e96';
			that.jsonApiService.http("stats/carlist.html", reqdata).subscribe(data => {
				if(data.success){
					var carlist = data.data;
		        	var selectdata = [];
		        	for(var i = 0 ; i < carlist.length ; i++) {
		        		var obj = {id:carlist[i].deviceId,text:carlist[i].carNumber};
		        		selectdata[i] = obj;
		        	}
		        	that.carList = selectdata;
		        	resolve();
				}else{reject();}
			})
		})
	}
	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load(){
		this.startTime = $('#datetimepicker1').val();
		this.endTime = $('#datetimepicker2').val();
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"deviceId":"'+this.deviceId+'","startTime":"'+this.startTime+'","endTime":"'+this.endTime+'"}}'
		this.jsonApiService.http("carPanelHistory/carpanelforpage.html", reqdata).subscribe(data => {
			if(data&&data.exhibitDatas){
				for(let item of data.exhibitDatas){
					item.loadWeight= common.twoDecimal(item.loadWeight);
				}
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//删除
	deleteRow(row){
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("stats/deletetransportdata.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
}
